package com.common.library.util;

public class RC4Encrypt {
	private static String ENCRYPT_PWD = "tandy_fw_20120309172733";
	private static final int BOX_LEN = 256;
	private static final String DIGITAL = "0123456789ABCDEF";
	/**
	 * 设置加密密码
	 * @param encryptPassword
	 */
	public static void setEncryptPassword(String encryptPassword){
		ENCRYPT_PWD = encryptPassword;
	}
	/**
	 * 转换
	 * @param aInput
	 * @return
	 */
	private static String RC4(String aInput) {
		String aKey = ENCRYPT_PWD;
		int[] iS = new int[BOX_LEN];
		byte[] iK = new byte[BOX_LEN];
		for (int i = 0; i < BOX_LEN; i++) {
			iS[i] = i;
		}
		int j = 0;
		for (short i = 0; i < BOX_LEN; i++) {
			iK[i] = (byte) aKey.charAt((i % aKey.length())); // charAt函数的作用是返回指定索引位置处的字符
		}
		for (int i = 0; i < BOX_LEN - 1; i++) {
			j = (j + iS[i] + iK[i]) % BOX_LEN;
			int temp = iS[i];
			iS[i] = iS[j];
			iS[j] = temp;
		}
		int i = 0;
		j = 0;
		char[] iInputChar = aInput.toCharArray();
		char[] iOutputChar = new char[iInputChar.length];
		for (short x = 0; x < iInputChar.length; x++) {
			i = (i + 1) % BOX_LEN;
			j = (j + iS[i]) % BOX_LEN;
			int temp = iS[i];
			iS[i] = iS[j];
			iS[j] = temp;
			int t = (iS[i] + (iS[j] % BOX_LEN)) % BOX_LEN;
			int iY = iS[t];
			char iCY = (char) iY;
			iOutputChar[x] = (char) (iInputChar[x] ^ iCY);
		}
		return new String(iOutputChar);
	}
	/**
	 * 加密
	 * @param aInput 待加密的字符串
	 * @return
	 */
	public static String encrypt(String aInput) {
		String str = RC4(aInput);
		str = bin2hex(str);
		return str;
	}
	/**
	 * 解密
	 * @param aInput 待解密的字符串
	 * @return
	 */
	public static String decrypt(String aInput) {
		aInput = hex2bin(aInput);
		String str = RC4(aInput);
		return str;
	}

	/**
	 * 字符串转换成十六进制值
	 * 
	 * @param bin
	 *            String 我们看到的要转换成十六进制的字符串
	 * @return
	 */
	private static String bin2hex(String bin) {
		char[] digital = DIGITAL.toCharArray();
		StringBuffer sb = new StringBuffer("");
		byte[] bs = bin.getBytes();
		int bit;
		for (int i = 0; i < bs.length; i++) {
			bit = (bs[i] & 0x0f0) >> 4;
			sb.append(digital[bit]);
			bit = bs[i] & 0x0f;
			sb.append(digital[bit]);
		}
		return sb.toString();
	}

	/**
	 * 十六进制转换字符串
	 * 
	 * @param hex
	 *            String 十六进制
	 * @return String 转换后的字符串
	 */
	private static String hex2bin(String hex) {
		String digital = DIGITAL;
		char[] hex2char = hex.toCharArray();
		byte[] bytes = new byte[hex.length() / 2];
		int temp;
		for (int i = 0; i < bytes.length; i++) {
			temp = digital.indexOf(hex2char[2 * i]) * 16;
			temp += digital.indexOf(hex2char[2 * i + 1]);
			bytes[i] = (byte) (temp & 0xff);
		}
		return new String(bytes);
	}

	public static void main(String[] args) {
		String inputStr = "abcdefghijklmnopqrstuvwxyz_1234567890";
		String str = encrypt(inputStr);
		String decryptStr = decrypt(str);
		System.out.println("Before Encrypt: " + inputStr);
		System.out.println("After  Encrypt: " + str);
		System.out.println("After  Decrypt: " + decryptStr);
		System.out.println(inputStr.equals(decryptStr) ? "Right" : "Wrong");
	}
}
